package com.hainatong.shiphelper.module.OAmgr;


import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ActivityShenpiBinding;
import com.hainatong.shiphelper.module.OAmgr.adapter.ApplyItemAdapter;
import com.hainatong.shiphelper.module.OAmgr.entity.ApplyItemBean;
import com.hainatong.shiphelper.widget.dropdownmenu.DropDownMenu;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.hainatong.mvvmbase.base.NoViewModel;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ShenPiActivity extends BaseMvvmActivity<ActivityShenpiBinding, NoViewModel>
{
    private SmartRefreshLayout refreshLayout;

    private RecyclerView recyclerView;

    private ApplyItemAdapter adapter;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_shenpi;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView()
    {
        setToolbarTitle( viewDataBinding.toolbar );
        setToolBar( viewDataBinding.toolbar );
        // 搜索
        viewDataBinding.btnSearch.setOnClickListener( v -> {
            String keyWordStr = viewDataBinding.etSearch.getText().toString().trim();
            if(!TextUtils.isEmpty( keyWordStr ))
            {
                KeyboardUtils.hideSoftInput( ShenPiActivity.this );
                startSearchKeyName( keyWordStr );
            }
        } );
        viewDataBinding.etSearch.setOnKeyListener((v, keyCode, event) -> {
            if(keyCode == KeyEvent.KEYCODE_ENTER)
            {
                String keyWordStr = viewDataBinding.etSearch.getText().toString().trim();
                if(!TextUtils.isEmpty( keyWordStr ))
                {
                    KeyboardUtils.hideSoftInput( ShenPiActivity.this );
                    startSearchKeyName( keyWordStr );
                }
                return  true;
            }
            return false;
        });



        List<String> tabTitle = new ArrayList<>();
        tabTitle.add( "审批类型" );
        tabTitle.add( "船舶名称" );
        tabTitle.add( "部门" );

        HashMap<String,Object> shen = new HashMap<String,Object>();
        shen.put(DropDownMenu.KEY,DropDownMenu.TYPE_LIST_SIMPLE);
        shen.put(DropDownMenu.VALUE,new String[]{"值班交接","离职交接","上下船交接", "报废申请"} );

        HashMap<String,Object> ship = new HashMap<String,Object>();
        ship.put(DropDownMenu.KEY,DropDownMenu.TYPE_LIST_SIMPLE);
        ship.put(DropDownMenu.VALUE,new String[]{"班交接","职交接","下船交接"});


        HashMap<String,Object> men = new HashMap<String,Object>();
        men.put(DropDownMenu.KEY,DropDownMenu.TYPE_LIST_SIMPLE);
        men.put(DropDownMenu.VALUE,new String[]{"职接","船交接","废请"});

        List<HashMap<String,Object>> viewDatas = new ArrayList<>();
        viewDatas.add( shen );
        viewDatas.add( ship );
        viewDatas.add( men );

        View listContent = getLayoutInflater().inflate( R.layout.fragment_recyclerview,null );
        refreshLayout = listContent.findViewById( R.id.refreshlayout );
        recyclerView = listContent.findViewById( R.id.recyclerview );

        refreshLayout.setRefreshHeader( new ClassicsHeader(this));
        refreshLayout.setRefreshFooter( new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener( refreshLayout1 -> {

            refreshLayout1.finishRefresh( true );
        } );
        refreshLayout.setOnLoadMoreListener( refreshLayout1 -> {
            refreshLayout1.finishLoadMore( true );
        } );

        recyclerView.setLayoutManager( new LinearLayoutManager(this));
        recyclerView.addItemDecoration( new ListItemDecoration(SizeUtils.dp2px( 10f )));
        recyclerView.hasFixedSize();
        adapter = new ApplyItemAdapter();
        recyclerView.setAdapter( adapter );

        viewDataBinding.dropDownMenu.setDropDownMenu(tabTitle, viewDatas,  listContent );
        viewDataBinding.dropDownMenu.addMenuSelectListener( (index, pos, clickstr) -> {

            ToastUtils.showShort( "index = "+ index + "  pos = " + pos + " str = " + clickstr );

        } );


    }

    @Override
    protected void initData()
    {
        List<ApplyItemBean> itemBeanList = new ArrayList<ApplyItemBean>();
        itemBeanList.add(  new ApplyItemBean()  );
        itemBeanList.add(  new ApplyItemBean()  );
        itemBeanList.add(  new ApplyItemBean()  );
        itemBeanList.add(  new ApplyItemBean()  );
        itemBeanList.add(  new ApplyItemBean()  );
        itemBeanList.add(  new ApplyItemBean()  );

        adapter.setList( itemBeanList );

    }

    private void startSearchKeyName(String keyName)
    {
        ToastUtils.showShort( "搜索"+keyName );
    }

    private void setToolbarTitle(Toolbar toolbar)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }

}
